//
//  PKRootRightViewController.m
//  PianKe
//
//  Created by lanou on 15/12/21.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKRootRightViewController.h"

@interface PKRootRightViewController ()

@end

@implementation PKRootRightViewController

-(void)dealloc
{
    NSLog(@"rightVC 被销毁了");
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
#pragma mark ---- 创建自定义的导航栏
    self.barView = [[PKNaviBarView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    
    [self.barView.changeFrameButton addTarget:self action:@selector(changeFrame:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:self.barView];
    
    // 给label赋值
    self.barView.titleLabel.text = self.titleName;
    
    // 添加屏幕边缘 边缘滑动手势 来偏移View的位置
    self.edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanAction:)];
    
    self.edgePan.edges = UIRectEdgeLeft; // 从左往右扫
    
    [self.navigationController.view addGestureRecognizer:self.edgePan];
    
    // 设置手势,点击空白处让view复位
    self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBack:)];
    // 一开始在左边,不加tap (不然会和普通的交互重叠)
    // [self.navigationController.view addGestureRecognizer:self.tap];
    
    //    //设置loading
  //  [self p_setupProgressHud];
    
    
    
    
}



#pragma mark --- 设置Loading效果
- (void)p_setupProgressHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
   // _hud.frame = self.view.bounds;
    // 设置frame
     _hud.frame = CGRectMake((kScreenWidth - 100*kWidthBase) / 2, (kScreenHeight - 100*kHeightBase) / 2, 100*kWidthBase, 100*kWidthBase);
    _hud.minSize = CGSizeMake(100*kWidthBase, 100*kWidthBase);
   // _hud.backgroundColor = [UIColor redColor];
    _hud.mode = MBProgressHUDModeCustomView;
    [self.view addSubview:_hud];
    
    [_hud show:YES];
}

#pragma mark ---- 点击空白处让view复位
- (void)tapBack:(UITapGestureRecognizer *)tap
{
    CGRect newFrame = self.navigationController.view.frame;
    newFrame.origin.x = 0;
    
    [self.navigationController.view removeGestureRecognizer:tap];
    
    // 添加动画
    [UIView animateWithDuration:0.5 animations:^{
       
        self.navigationController.view.frame = newFrame;
        
    }];
    
    
}

#pragma mark --- 边缘滑动的方法
// 实现 边缘滑动的方法 : 现在要连导航栏一起滑动,所以改变的是navc
- (void)edgePanAction:(UIScreenEdgePanGestureRecognizer *)edgePan
{
    
    // view的选择应该是一个不动的view,所以选择superView,获取手势在rootVC.view的位置
    // 判断说是滑动时手指的x坐标
    CGFloat x = [edgePan locationInView:self.navigationController.view.superview].x;
    // NSLog(@"%lf",x);
    // 根据手势位置改变自身的view (右侧视图)的位置
    CGRect newFrame = self.navigationController.view.frame;
    
    if (x < kScreenWidth - 50) {
        // 加一个范围保护
        newFrame.origin.x = x;
        self.navigationController.view.frame = newFrame;

    }
    
    
 //   if (edgePan.edges == UIRectEdgeLeft) {
        
        // 位置随手势变化后 再判断
        if (edgePan.state == UIGestureRecognizerStateEnded) {
            // 如果当前手势结束了
            if (x > 100) {
                newFrame.origin.x = kScreenWidth - 50;
                // 添加手势
                [self.navigationController.view addGestureRecognizer:self.tap];
                
            } else {
                newFrame.origin.x = 0;
                // 关闭手势
                [self.navigationController.view removeGestureRecognizer:self.tap];
               
            }

        }
        
        // 2.UIView直接调用
        [UIView beginAnimations:@"changeFrame" context:nil];
        [UIView setAnimationDuration:0.5];
        // 动画的速度方式
        [UIView setAnimationCurve:(UIViewAnimationCurveLinear)];
        self.navigationController.view.frame = newFrame;
        [UIView commitAnimations];

  //  }
 
}



- (void)changeFrame:(UIButton *)button
{
    
    NSLog(@"右移");
    // 改变view的frame, 只需要修改起始位置的x就行了
    // 进行修改navigaionController的view
    CGRect newFrame = self.navigationController.view.frame;
    if (newFrame.origin.x == 0) {
        
        newFrame.origin.x = kScreenWidth - 50;
        
        // 重新添加tap手势
        [self.navigationController.view addGestureRecognizer:self.tap];
       
    } else
    {
        newFrame.origin.x = 0;
        // 移出手势
        [self.navigationController.view removeGestureRecognizer:self.tap];
    }
   
    
    // 2.UIView直接调用
    [UIView beginAnimations:@"changeFrame" context:nil];
    [UIView setAnimationDuration:0.5];
    // 动画的速度方式:可以修改变frame时候的速度
    [UIView setAnimationCurve:(UIViewAnimationCurveLinear)];
    self.navigationController.view.frame = newFrame;
    [UIView commitAnimations];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
